<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';
include __DIR__ . '/mainpage.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';
$basketbalfunctions = new BasketbalFunctions();

$articleID = $_POST['articleID'];

$sql = "SELECT title  
		FROM articles
		WHERE ID = '$articleID'";

$result = $db->query($sql);
$row = $result->fetch_assoc();
$title = $row['title'];

?>

<div class="container-fluid">
	
	<div class="row">
		<div class="col-md-4"></div>
		<div class="col-md-4">

			        <p>
        <a class="btn btn-default btn-sm" href="./articles_archive.php">Back to Archive</a>
        <a class="btn btn-default btn-sm" href="./article_add.php">Add New Article</a>
        </p>

			<h4>Visibility of Article "<?=$title?>" </h4>

			<div id="currentvisibility"> </div>

			<h4>Add visibility:</h4>

			<form method="POST" action="<?= $_SERVER["PHP_SELF"];?>">
				
				<input type="hidden" name="articleID" value="<?= $articleID ?>" >


	<select name="season" class="form-control" id="selectseason">

<?php if (isset($_POST['season'])) {   
	$setseason = $_POST['season'];

?>

<option id="selected-season" value="<?=$setseason?>" selected="selected"><?=$setseason?> is selected</option>";


<?php   } else { ?>		
	    <option id="selected-season" value="" disabled="disabled" selected="selected">Select a season</option>";

<?php

}

$sql = "SELECT DISTINCT season 
		FROM teams
		ORDER BY season";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
				$season = $row['season'];
				echo "<option value=\"$season\">$season</option>";
};
?>

	</select>

	<br/> 

<?php 

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];


?>   
      
          <select class="form-control" name="user_key">
         	<option value="" disabled="disabled" selected="selected">Select a player</option>

<?php

  $sql = "SELECT users.firstname, users.lastname, users.user_key, teams.season, teams.playerID
  FROM users
  JOIN teams
  ON users.user_key = teams.playerID
  WHERE teams.season = '$setseason'
  ORDER BY users.firstname";

  $result=$db->query($sql);

  while($row=$result->fetch_assoc()){	

    $user_key=$row['user_key']; 
    $firstname=$row['firstname']; 
    $lastname=$row['lastname']; 
  
  echo "<option value=\"$user_key\">$firstname $lastname</option>";

};

  ?>
          </select>

				<div id="success_message"></div>
				<br/>
				<button id="addvis" class="btn btn-success btn-lg btn-block"> Add</button>

<?php } ?>
			</form>

		</div>
		<div class="col-md-4"></div>

	</div>
</div>

<script type="text/javascript">

var articleID = <?= $articleID ?>;

function LoadArticleVisility(){
		var articleID = <?= $articleID ?>;
	    $.ajax({
			    url: "./article_visibility_current.php",
			    method: "POST",
			    data: { "articleID": articleID},
			    success:function(data){
			      $('#currentvisibility').html(data);
			    }, 
			    error: function (xhr, status, error) {
			          alert(xhr.responseText); 
			    }
			    });
};

$(document).on("change","#selectseason", function(e){
	this.form.submit();
});


$(document).on("click", ".delete_visibility",function(e) {
		postData = $(this.form).serialize();
		var ID = $(this.form).find('input[name="ID"]').val();

		if( confirm("Are you sure to delete this visibility?")){
		    $.ajax({
		      type: "POST",
		        url: "./includes/handlers/article_visibilty_delete_handler.php",
		        data: postData,
		        success: function(data) {    
		          $("#tablerow" + ID).html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#tablerow" + ID).html(xhr.responseText);
		        }
		    });
		 } 
		e.preventDefault();		
	});



$(function() {
	LoadArticleVisility();
});

$(document).on('click', '#addvis', function(event){

    event.preventDefault();
    var form_data = $(this.form).serialize();

    $.ajax({
    url: "./includes/handlers/article_visibility_add_handler.php",
    method: "POST",
    data: form_data,
    beforeSend:function(){
     $('#submit').val('Submitting...');
    },
    success:function(data){
     if(data != '')
     {
      $('#success_message').html(data);
      	LoadArticleVisility();
     }
    }, 
    error: function (xhr, status, error) {
          alert(xhr.responseText); 
    }
    });
    });

</script>

